<div class="layui-row layui-col-space15">
  <div class="layui-col-md12">
	<div class="layui-card hxr-layui-card">
		<div class="layui-form hxradmin-card-header">
			<div class="layui-form-item">
				<div class="layui-inline">
					<label class="layui-form-label">院系名称</label>
					<div class="layui-input-inline">
						<input type="text" name="users" autocomplete="off" class="layui-input">
					</div>
				</div>
				<div class="layui-inline">
					<button class="layui-btn" data-type="reload" id="searchs">搜索</button>
				</div>
			</div>
		</div>
	  <div class="layui-card-body">
		<table class="layui-hide" id="list-select-table" lay-filter="list-table"></table>
	  </div>
	</div>
  </div>
</div>
<div class="form-btn" style="bottom: -20px;">
	<input type="button" value="确认选择" class="layui-btn selectok">
 </div>
<script>
   //表格数据渲染
  layui.use(['table','form'], function () {
	  var table = layui.table
	  ,form = layui.form
	  ,$ = layui.$,ids,names;
	  ids=layui.admin.schools.institute_id?layui.admin.schools.institute_id.split(','):'';
	  names=layui.admin.schools.institute_name?layui.admin.schools.institute_name.split(','):'';
	  layui.admin.initlist(table,'schools/institutes_list',195,[[
		{type:'checkbox'}
        ,{field:'institute_id', title: 'ID', width:60}
        ,{field:'institute_name', title: '院系名称', minWidth:200}
        ,{field:'create_time', title: '创建时间', width: 200}
      ]],'#list-select-table',380,'',{school_id:layui.admin.schools.school_id,'agent_id':layui.admin.agent.agent_id},{obj:layui.admin.schools.institute_id,val:'institute_id'},false,500);

	  // table.on('checkbox(list-table)', function(obj){
	  //
		//
		//
		//
		//
	  //
		//   if (obj.type=='all'){
		// 	  var data_all=table.cache['list-select-table'];
		//   	if (obj.checked){
		// 		for(var i=0; i<data_all.length;i++) {
		// 			var institute_id=data_all[i].institute_id.toString();
		// 			if (ids.indexOf(institute_id)<0){
		// 				ids.push(institute_id);
		// 				names.push(data_all[i].institute_name);
		// 			}
		// 		}
		// 	}else{
		// 		for(var i=0; i<data_all.length;i++) {
		// 			var institute_id=data_all[i].institute_id.toString();
		// 			if (ids.indexOf(institute_id)>=0){
		// 				ids.splice(ids.indexOf(institute_id),1);
		// 				names.splice(names.indexOf(data_all[i].institute_name),1);
		// 			}
		// 		}
		// 	}
		//   }else{
		// 	  var institute_id=obj.data.institute_id.toString();
		// 	  if (obj.checked){
		// 		  if (ids.indexOf(institute_id)<0){
		// 			  ids.push(institute_id);
		// 			  names.push(obj.data.institute_name);
		// 		  }
		// 	  }else {
		// 		  if (ids.indexOf(institute_id)>=0){
		// 			  ids.splice(ids.indexOf(institute_id),1);
		// 			  names.splice(names.indexOf(obj.data.names),1);
		// 		  }
		// 	  }
		//   }
		//   layui.admin.agent.institute_ids=ids.join(',');
		//
	  //
	  // });
	$('.selectok').click(function(){
		var checkStatus = table.checkStatus('list-select-table')
		,checkData = checkStatus.data,ids,names;
		for(var i=0; i<checkData.length;i++){
			if(ids){
				ids+=','+checkData[i].institute_id;
				names+=','+checkData[i].institute_name;
			}else{
				ids=checkData[i].institute_id;
				names=checkData[i].institute_name;
			}
		}
		if(!ids){
			layui.admin.ahint(2,'请选择负责院系');
		}else{
			layui.admin.schools.institute_id=ids;
			layui.admin.schools.institute_name=names;
			layer.close(layui.admin.pups);
		}
	});
	//搜索
	$('#searchs').on('click', function() {
		var val1 = $('input[name="users"]').val()
		//执行重载
		table.reload('list-select-table', {
			page: {
				curr: 1 //重新从第 1 页开始
			},
			where: {
				search_text: val1
			}
		});
	});
  });
</script>